<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['lang']){
	$_SESSION['show_lang']=$_RQ['lang'];
}
$return['mid']=$_SESSION['uid'];
$return['show_setmobile']=cfc('core')->check_setmobile(1);
$return['show_setmember']=cfc('core')->check_setmember(1);
$merchid=$_SESSION['merchid']?:$this->session('merchid');
if($_RQ['op']=='main'){
	$shopset=plugin('jy_weishop')->plugin_setting('shopset');
	if($shopset['time']){
		$daytime=date("Y-m-d");
		if($shopset['time']['start']){
				$start=$daytime.' '.$shopset['time']['start'];
				$start=strtotime($start);
				if($start>time()){
					exi('营业时间未开始，不能下单！','error','/index/');
				}
		}
		if($shopset['time']['end']){
				$end=$daytime.' '.$shopset['time']['end'];
				$end=strtotime($end);
				if($end<time()){
					exi('营业时间已过，不能下单！','error','/index/');
				}
		}
	}
	if($_RQ['goodsid']){
		$login_burl='jy_textfee/goods.detail//id='.$_RQ['goodsid'];
		$this->session('login_burl',$login_burl);
	}
	$return=model('jy_weishop/get_order')->getdata($_RQ);
	if($_SESSION['openid'.$merchid] || $_SESSION['onemerchid']){
		$return['onemerchid']=$_SESSION['onemerchid']?:$merchid;
	}
	if($_RQ['out_time']){
		$return['post']['out_time']=$_RQ['out_time'];
	}
	if($_RQ['dayid']){
		$return['post']['dayid']=$_RQ['dayid'];
	}
	$return['zxout_time']=plugin('jy_weishop')->model('store')->zxout_time();
	$return['post']['sendtype']=2;
	$jy_commission=$this->plugin_exists('jy_commission');
	$groupaddfee=0;
	foreach($return['order']['list'] as &$item){
		if($jy_commission){
			$item['groupaddfee']=model('jy_commission/model')->getgroupaddfee($item);
			$item['price'] +=$item['groupaddfee'];
			$groupaddfee +=$item['groupaddfee'];
		}
		$item['api_data']=json_decode($item['api_data'],true);
		if($item['api_data']['dingpei']){
			$return['post']['is_dingpei']=1;
			break;
		}
	}
	$return['order']['out_upfee']=$this->model('model')->getbaofee($return['order']['list']);
	if($groupaddfee>0 || !$order['is_peisong']){
		$order=$return['order'];
		$order['total_price'] +=$groupaddfee;
	//会员折扣价
		$order['level_de_price']=model('jy_weishop/activity')->get_level_de_price($order);
		//运势折扣价
		$order['luck_de_price']=model('jy_weishop/activity')->get_luck_de_price($order);
		//满减优惠价格：
		$saleset=$this->plugin_setting('saleset');
		$sale_enough=$saleset['enough'];
		if($sale_enough){
			foreach($sale_enough as $k=>$ev){
				if($ev['man']>$order['total_price']){
					unset($sale_enough[$k]);
				}
			}
			paixu($sale_enough,'man','desc');
		}
		$order['sale_enough']=$sale_enough;
		$order['enough_de_price']=$sale_enough[0]['jian']?$sale_enough[0]['jian']:0.00;
		//送积分
		$order['credit']=model('jy_weishop/activity')->get_buy_credit_order($order);
		//送虚拟币
		$order['credit3']=model('jy_weishop/activity')->get_buy_credit3_order($order);
		//积分抵扣
		$deduct_res=model('jy_weishop/activity')->get_deduct_credit_order($order,$saleset,$member);
		$order['deduct_credit']=$deduct_res['deduct_credit'];
		$order['delcredit1']=$deduct_res['delcredit1'];
		//优惠券优化价格：
		$order['coupon_de_price']=0.00;
		if(!$order['no_coupon_discount']){
			//整理优惠券数据，先获取用户所有待使用优惠券，再从中筛选出未过期的优惠券且本次下单可用的优惠券！
			$newcoupons=model('jy_weishop/coupon')->getmycoupon_ing(1,$order['total_price'],$order['level_de_price'],$order['list'],$order['enough_de_price']);
			$order['coupon_de_price']=$newcoupons[0]['de_price'];
			$item['couponid']=$newcoupons[0]['id'];
			$return['couponcount']=count($newcoupons);
		}
		$return['coupons']=$newcoupons?$newcoupons:array();
		$order['endprice']=round($order['total_price'] + $order['dispatchprice'] + $order['out_upfee'] - $order['level_de_price'] - $order['luck_de_price']-$order['buyxiu_de_price']  - $order['enough_de_price'] - $order['deduct_credit'],2);
		$order['no_dispatch_price']=round($order['total_price'] + $order['out_upfee'] - $order['level_de_price'] - $order['luck_de_price']-$order['buyxiu_de_price'] - $order['enough_de_price'] - $order['deduct_credit'],2);
		$order['endprice']=$order['endprice']>=0?$order['endprice']:0;
		$return['post']['endprice']=$order['endprice']- $order['coupon_de_price'];
		$return['post']['endprice']=$return['post']['endprice']>=0?$return['post']['endprice']:0;
		$return['post']['no_dispatch_price']=$order['no_dispatch_price']- $order['coupon_de_price'];
		$order['no_dispatch_price2']=$return['post']['no_dispatch_price'];
		$return['post']['no_dispatch_price']=$return['post']['no_dispatch_price']>=0?$return['post']['no_dispatch_price']:0;
		$order['no_dicard_endprice']=$return['post']['endprice'];
		$order['no_dicard_dispatch_endprice']=$return['post']['no_dispatch_price'];
		//购物卡抵扣
		$last_cardfee=model('jy_weishop/goodscard')->getorderfee();
		if($last_cardfee>0){
			$order['last_cardfee']=$last_cardfee;
			$return['post']['endprice']=$order['last_cardfee']>$return['post']['endprice']?0:$return['post']['endprice']-$order['last_cardfee'];
			$return['post']['no_dispatch_price']=$order['last_cardfee']>$return['post']['no_dispatch_price']?0:$return['post']['no_dispatch_price']-$order['last_cardfee'];
		}
		$_SESSION['order']=$order;
		$return['order']=$order;
	}
	if($this->plugin_exists('jy_aiaddress')){
		$return['jy_aiaddress']=plugin('jy_aiaddress')->plugin_setting('basic');
	}
	//返回链接
	$return['forward_url']=base64_encode('jy_textfee/order.list//fromcreate=1');
	set_html('title','确认订单');
	exi($return);
}
// 完成下单
if($_RQ['op']=='post'){
	$post=get_postdata();
	//整理订单数据
	$post['dayid']=$post['dayid']?$post['dayid']:0;
	//配送时间
	$days = array(
				0=>date("Y-m-d",time("day")),
				1=>date("Y-m-d",strtotime("+1 day")),
				2=>date("Y-m-d",strtotime("+2 day")), 
				3=>date("Y-m-d",strtotime("+3 day")), 
				4=>date("Y-m-d",strtotime("+4 day")),
				5=>date("Y-m-d",strtotime("+5 day")),
				6=>date("Y-m-d",strtotime("+6 day")),
	);
	$dayid=intval($post['dayid']);
	$post['postout_time']=$post['out_time'];
	$post['daytime']=$days[$dayid];
	$post['out_time']=$days[$dayid].' '.$post['out_time'];
	$new_order=model('jy_weishop/get_order')->new_order($post);
	$new_order['plugin_name']='jy_textfee';
	if(!$new_order['merchid'] && $_SESSION['onemerchid']){
		$new_order['api_data']=array('dhmerchid'=>$_SESSION['onemerchid']);
	}
	$order_id=model('jy_weishop/order')->create($new_order);
	if($_SESSION['current_plugin'] && $_SESSION['current_plugin']!='core' && $_SESSION['current_plugin']!='jy_weishop'){
		$success_url=plugin($_SESSION['current_plugin'])->weishop_order_detail;
		if($success_url){
			$success_url=$_SESSION['current_plugin'].$success_url.'/id='.$order_id;
			$error_url=$success_url;
		}
	}
	$params=array(
		'id'=>$order_id,
		'title'=>'购买商品',
		'fee'=>$new_order['price'],
		'oldfee'=>$new_order['oldfee'],
		//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
		//'no_type'=>array('hdfk'),//可选，此项作用为指定不允许使用哪种支付方式
		'back_url'=>'jy_textfee/order.create/payresult',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
		'success_url'=>$success_url?:'jy_textfee/order.list/',
		'error_url'=>$error_url?:'jy_textfee/order.list/',
	);
	//虚拟设置
	$plugin_xuni=model('jy_weishop/activity')->plugin_xuni($new_order);
	if(count($plugin_xuni)){
		$params['plugin_xuni']=$plugin_xuni;
	}
	if($new_order['merchid']){
		$merchset=plugin('jy_merch')->plugin_setting('setting');
	}
	if($merchset['isone_credit'] && $new_order['merchid']){
		$params['merchid']=$new_order['merchid'];
	}
	$payid=cfc('pay')->create_pay_order($params);
	pdo_update('jy_weishop_order',array('pay_id'=>$payid),array('id'=>$order_id));
	if($post['peerpay']){
		$peerpay_setting=plugin('jy_weishop')->plugin_setting('peerpay');
		if($peerpay_setting && $peerpay_setting['open']){
			exi('','','jy_weishop/order.peerpay//orderid='.$order_id);
		}
	}
	$res=array(
		'pay_id'=>$payid,
		'order_id'=>$order_id,
		'success_url'=>$success_url,
		'error_url'=>$error_url,
	);
	exi($res);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if($payresult['status']!=1 && $payresult['pay_type'] !='hdfk'){
		exi('支付失败。','error','jy_textfee/order.list/');
	}
	$order=pdo_get('jy_weishop_order',array('id'=>$payresult['id']));
	if($order['ispay'] ==1){
		exi('该订单已经支付！','error','jy_textfee/order.list/');
	}
	if($order['price']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！'.$order['price'].'|'.$payresult['fee'],'error','jy_textfee/order.list/');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
	);
	model('jy_weishop/order')->order_pay($order['id'],$params);
	$message='有新的订单支付成功！';
	if($order['merchid']){
	}else{
		model('jy_commission/commission')->upgroupaddfee($order);
	}
	exi('提交成功!');
}

?>